package com.gr

class PhotoUploadCommand {
    byte[] photo
    String userId
}

class ImageController {

    def upload = { PhotoUploadCommand puc ->
        println "Starting upload"
        def user = User.findByUserId(puc.userId)
        user.profile.photo = puc.photo
        redirect(action: 'view', id: puc.userId)
    }
	
	def rawUpload = {

		// a Spring MultipartHttpServletRequest
		def mhsr = request.getFile('photo')
		if(!mhsr?.empty && mhrs.size < 1*1024*1024) { // 1MB
			mhsr.transferTo(
					new File('/hubbub/images/${request.userId}/mugshot.gif') )

		}
	}
	
	def renderImage = {
		def user = User.findByUserId(params.id)
		if (user && user?.profile?.photo) {
			response.setContentLength(user.profile.photo.length)
			response.getOutputStream().write(user.profile.photo)
		} else {
			response.sendError(404)
		}

	}

    def form = {
        // pass through to upload form
		[ userList : User.list() ]
    }

    def view = {
        // path through to "view photo" page
    }

}
